import logging

from src.db.api import TabLeOperator
from src.db.table import Order

LOG = logging.getLogger()


def add_order(order):
    find_order = {'order_number': order['order_number']}
    order_list = TabLeOperator(Order).get(find_order)
    if order_list:
        LOG.warning('order number {} have added'.format(order['order_number']))
    else:
        TabLeOperator(Order).add(order)


def set_ship_flag(order_number):
    find_order = {'order_number': order_number}
    order_list = TabLeOperator(Order).get(find_order)
    if order_list:
        update_order = {'order_number': order_number, 'ship_flag': True}
        TabLeOperator(Order).update(update_order)
    else:
        err_msg = "not find order_number {}, can't ship".format(order_number)
        LOG.error(err_msg)
        raise Exception(err_msg)


def set_ship_text_flag(order_number):
    find_order = {'order_number': order_number}
    order_list = TabLeOperator(Order).get(find_order)
    if order_list:
        update_order = {'order_number': order_number, 'ship_text': True}
        TabLeOperator(Order).update(update_order)
    else:
        err_msg = "not find order_number {}, can't ship".format(order_number)
        LOG.error(err_msg)
        raise Exception(err_msg)


if __name__ == '__main__':
    order_number = '1212096865390528788'
    set_ship_flag(order_number)
    set_ship_text_flag(order_number)
    filter_dict = {'order_number': order_number}
    p = TabLeOperator(Order).get(filter_dict)
    print(p)
